import { API_MODEL, APP_ID, UNION_ID, VERSION } from "@/utils/constants";
import { baseHeaders } from "@/utils/request";

const modelApi = {
  categories: async function () {
    const model = await fetch(`${API_MODEL}/client/api/v1/categories`, {
      method: "GET",
      headers: {
        ...baseHeaders(),
        appid: APP_ID,
        unionid: UNION_ID,
        version: VERSION,
      },
    })
      .then((res) => res.json())
      .catch(() => []);
    return model;
  },

  list: async function (firstId, secondId, page, pageSize) {
    const model = await fetch(
      `${API_MODEL}/client/api/v1/item?first_category_id=${firstId}&second_category_id=${secondId}&page=${page}&pageSize=${pageSize}`,
      {
        method: "GET",
        headers: {
          ...baseHeaders(),
          appid: APP_ID,
          unionid: UNION_ID,
          version: VERSION,
        },
      }
    )
      .then((res) => res.json())
      .catch(() => []);
    return model;
  },

  sign: async function (data) {
    const model = await fetch(`${API_MODEL}/client/api/v1/signurl`, {
      method: "POST",
      headers: {
        ...baseHeaders(),
        "Content-Type": "application/json",
        appid: APP_ID,
        unionid: UNION_ID,
        version: VERSION,
      },
      body: JSON.stringify(data),
    })
      .then((res) => res.json())
      .catch(() => []);
    return model;
  },
};
export default modelApi;
